﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;

namespace TestData
{
    public class RND
    {
        
            protected static int LastNum = 851464321;
            protected static int Cnt = 0;
            protected static int LastSeed = 0;
            protected static int TimeSeed = 0;
            public static string SeedDebug;
            private static Random _rnd;

            private static Random rnd
            {
                get {
                    
                    Cnt += 1;
                    long TimeSeed = DateTime.Now.Ticks;
                    //Debug.WriteLine("ticks:" + ticks.ToString());
                    //string ticks = TimeSeed.ToString();
                    //TimeSeed = tick.Substring(0, 5);
                    TimeSeed = DateTime.Now.Millisecond; // int.Parse(tick);
                    LastSeed = (int)(Cnt * 10) + (int)LastNum + (int)TimeSeed;

                    if (Cnt == 1)
                    {
                        _rnd = new Random(LastSeed);
                        SeedDebug = String.Format("seed:{0},cnt:{2},tick:{3},lastNum:{1}", LastSeed.ToString(), LastNum.ToString(), Cnt.ToString(), TimeSeed.ToString());

                    }
                    if (Cnt % 10 == 0)
                    {
                        _rnd = new Random(LastSeed);
                        SeedDebug = String.Format("seed:{0},cnt:{2},tick:{3},lastNum:{1}", LastSeed.ToString(), LastNum.ToString(), Cnt.ToString(), TimeSeed.ToString());

                    }

                    return _rnd;
                }
            }
            public static void Randomize()
            {
                int num = RND.GetRandomInt(1, 10);
                for (int i = 0; i< num; i++)
                {
                    int num2 = RND.GetRandomInt(1, 10);
                    for (int j = 0; j < num2; j++)
                    {
                        int num3 = RND.GetRandomInt(1, 1000);
                    }
                }
            }
            public static int GetRandomInt(int minVal, int maxVal)
            {
                maxVal += 1;
                int num = rnd.Next(minVal, maxVal);
                LastNum = num + Cnt;
                if (num > maxVal)
                    num = maxVal;
                return num;
            }
            public static double GetRandomDouble(int minVal, int maxVal)
            {
               
                double num = rnd.Next(minVal, maxVal);
                num = num + rnd.NextDouble();
                LastNum = (int)num + Cnt;
                return num;

            }
            public static string GetRandomText(int lenght)
            {
                Int32 startNum = 97;
                int endNum = 122;
                System.Text.StringBuilder sb = new System.Text.StringBuilder();
                int num = 0;
                for (int i = 0; i <= lenght - 1; i++)
                {
                    num = GetRandomInt(startNum, endNum);
                    char c = ((char)num);
                    sb.Append(c.ToString());
                }
                string ret = sb.ToString();
                return ret;
            }
            public static bool GetRandomBool()
            {
                int num = RND.GetRandomInt(1, 2);
                return num == 1;
            }
            public static string GetRandomTextWithFunkySymbols(int lenght)
            {
                Int32 startNum = 32;
                int endNum = 126;
                System.Text.StringBuilder sb = new System.Text.StringBuilder();
                for (int i = 0; i <= lenght - 1; i++)
                {
                    sb.Append(((char)GetRandomInt(startNum, endNum)).ToString ());
                }
                return sb.ToString();
            }
            public static string GetRandomNumericString(int length)
            {
                string toReturn = string.Empty;
                StringBuilder sb = new StringBuilder();
                for (int i = 0; i < length; i++)
                {
                    sb.Append(RND.GetRandomInt(0, 9).ToString());
                }
                toReturn = sb.ToString();
                return toReturn;
            }
            public static DateTime getRandomDate(DateTime minDate, DateTime maxDate)
            {
                TimeSpan ts = maxDate.Subtract(minDate);
                int num = GetRandomInt (0, ts.Days);
                return minDate.AddDays(num);
            }


    }
}
